package com.tonny.tweet.service.impl;

import java.util.Date;
import java.util.List;

import com.tonny.tweet.dao.TweetDao;
import com.tonny.tweet.enumration.TweetStatusEnum;
import com.tonny.tweet.model.Tweet;
import com.tonny.tweet.service.TweetService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TweetServiceImpl implements TweetService {
    private final Logger LOGGER = LoggerFactory.getLogger(TweetServiceImpl.class);
    
    @Autowired
    private TweetDao tweetDao;
    
    @Override
    public Tweet publish(long userId, String content) {
        Tweet tweet = new Tweet();
        tweet.setContent(content);
        tweet.setUserId(userId);
        Date now = new Date();
        tweet.setCreateTime(now);
        tweet.setStatus(TweetStatusEnum.NORMAL.getCode());
        tweet.setUpdateTime(now);
    
        LOGGER.debug("发布tweet, userId[{}], content[{}]", userId, content);
        tweetDao.insert(tweet);
        
        return tweet;
    }
    
    @Override
    public List<Tweet> allMyTweet(long userId) {
        return tweetDao.queryByUserId(userId, TweetStatusEnum.NORMAL.getCode(), 0, 1000);
    }
    
    @Override
    public boolean delete(long userId, long tweetId) {
        LOGGER.info("删除tweet, userId[{}], tweetId[{}]", userId, tweetId);
        tweetDao.updateStatus(tweetId, TweetStatusEnum.DELETED.getCode());
        return true;
    }
}
